package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.hubery.mapper.RoleMapper;
import xyz.hubery.pojo.Role;
import xyz.hubery.service.RoleService;

import java.util.List;

@DubboService
@Transactional
public class RoleServiceImpl implements RoleService {

    private static final Log log = LogFactory.getLog(RoleServiceImpl.class);
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long id) {
        // 删除用户-角色关联表
        roleMapper.deleteUserRoles(id);
        // 删除角色-权限关联表
        roleMapper.deleteRolePermissions(id);
        // 删除角色
        roleMapper.deleteById(id);
    }

    @Override
    public Role findById(Long id) {
        // 查询角色带权限
        return roleMapper.selectRoleWithPermissions(id);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public Page<Role> search(int page, int size) {
        return roleMapper.selectPage(new Page<Role>(page,size),null);
    }


    /**
     * 修改角色权限
     * @param rid 角色id
     * @param pids 权限id
     */
    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {
        // 删除角色权限关联表
        roleMapper.deleteRolePermissions(rid);
        // 添加角色权限关联表
        roleMapper.addRolePermissions(rid,pids);

        log.info("修改成功==========");
    }
}
